﻿using System;

namespace RuTrackerLib.Editors.Links.Editors
{
    class ParentLinkEditor : ILinkEditor
    {
        private readonly string _currentDirectory;

        public ParentLinkEditor(string currentDirectory)
        {
            if (string.IsNullOrEmpty(currentDirectory))
                throw new ArgumentNullException("currentDirectory");

            _currentDirectory = currentDirectory;
        }

        public Link EditLink(Link link)
        {
            Link newLink = link.Clone();
            newLink.HRef = link.HRef.Remove(0, 3);
            string parentFolder = WebFolderManager.GetParentFolder(_currentDirectory);

            if (newLink.HRef.StartsWith("../")) 
            {
                ParentLinkEditor editor = new ParentLinkEditor(WebFolderManager.GetParentFolder(parentFolder));
                return editor.EditLink(newLink);
            }

            return new GenericLinkEditor(parentFolder).EditLink(newLink);
        }
    }
}
